package com.hgm.travelapp.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.hgm.travelapp.R
import com.hgm.travelapp.bean.HomeListData

class RecyclerViewAdapter(var homeList: List<HomeListData>?) :
      RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>() {

      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            val view =
                  LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_gridlayout, parent, false)
            return MyViewHolder(view)
      }

      override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            val list = homeList?.get(position)

            if (position % 2 == 0) {
                  holder.item_head.setImageResource(R.drawable.bg1)
                  holder.item_title.text = "避暑山庄 冷泉滩"
                  holder.item_price.text = "￥${list?.price}起"
                  holder.item_position.text=list?.city
            } else {
                  holder.item_head.setImageResource(R.drawable.bg2)
                  holder.item_title.text = "小众海湾"
                  holder.item_price.text = "￥388起"
            }
            holder.item_content.isSingleLine =
                  (position == 0 || position == 3 || position == 4 || position == 7 || position == 9)
            holder.item_content.text = "经过千万年的地层变化，海风侵蚀形成了令人叹为观止，值得到此一游"

            holder.itemView.setOnClickListener {
                  mOnItemClickListener?.onItemClick(position)
            }
      }

      override fun getItemCount(): Int {
            return 20
      }

      inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            var item_head = view.findViewById<ImageView>(R.id.item_head)
            var item_title = view.findViewById<TextView>(R.id.item_title)
            var item_content = view.findViewById<TextView>(R.id.item_content)
            var item_price = view.findViewById<TextView>(R.id.item_price)
            var item_position = view.findViewById<TextView>(R.id.item_position)
      }

      var mOnItemClickListener: OnItemClickListener? = null

      fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
            mOnItemClickListener = onItemClickListener
      }

      interface OnItemClickListener {
            fun onItemClick(pos: Int) {}
      }
}